import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

/**
 * 
 * @author Luke Brown
 *
 */
public class Screen {
	
	@Override
	public String toString() {
		return "Screen [width=" + width + ", height=" + height + ", menu="
				+ menu + "]";
	}

	private BufferedImage bgImg;
	private int width;
	private int height;
	private ArrayList<Button> buttonList = new ArrayList<Button>();
	private ArrayList<Sprite> spriteList = new ArrayList<Sprite>();
	private Menu menu;

	/**
	 * @param bgImg the background image of the screen
	 */
	public Screen(BufferedImage bgImg, Menu menu){
		this.menu=menu;
		this.bgImg=this.resizeImage(bgImg, menu.getWidth(), menu.getHeight());
		this.setWidth(menu.getWidth());
		this.setHeight(menu.getHeight());
	}

	public void update(){
		for(Sprite s:spriteList){
			s.update();
			for(Sprite j:spriteList){
				if(s.equals(j)==false){
					if(s.isColliding(j)){
						s.onCollision();
						j.onCollision();
						System.out.println(s.getTlx()+","+j.getTlx());
					}
					
				}
			}
		}
	}
	
	public void addButton(Button b){
		buttonList.add(b);
	}
	
	/** 
	 * @param b the button to remove
	 */
	public void removeButton(Button b){
		ArrayList<Button> copy = new ArrayList<Button>();
		copy.addAll(buttonList);
		copy.remove(b);
		setButtonList(copy);
	}
	
	/**
	 * @param s the sprite to add to the spriteList
	 */
	public void addSprite(Sprite s){
		spriteList.add(s);
	}	
	
	/**
	 * @param s the sprite to remove from the spriteList
	 */
	public void removeSprite(Sprite s){
		ArrayList<Sprite> copy = new ArrayList<Sprite>();
		copy.addAll(spriteList);
		copy.remove(s);
		setSpriteList(copy);
	}

	/**
	 * @param g the specified Graphics2D surface to draw to
	 */
	public void paint(Graphics2D g){
		g.drawImage(getBgImg(), null, 0,0);
		for(Button b:getButtonList()){
			b.paint(g);
		}
		for(Sprite s:getSpriteList()){
			s.paint(g);
		}
	}

	/**
	 * @param mouseX the x position of the mouse
	 * @param mouseY the y position of the mouse
	 * @return the appropriate screen to go to
	 */
	public Screen mouseClicked(int mouseX,int mouseY){
		for(Button b:getButtonList()){
			if(b.isAbove(mouseX, mouseY)){
				return b.getGotoScreen();
			}
		}
		return this;
	}
	
	public BufferedImage resizeImage(BufferedImage img, int newW, int newH) {  
        int w = img.getWidth();  
        int h = img.getHeight();  
        BufferedImage dimg = new BufferedImage(newW, newH, img.getType());  
        Graphics2D g = dimg.createGraphics();  
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
        g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);  
        g.dispose();  
        return dimg;  
    }  

	//Getters and Setters
	/**
	 * @return the bgImg of the screen
	 */
	public BufferedImage getBgImg() {
		return bgImg;
	}
	/**
	 * @param bgImg the bgImg to set
	 */
	public void setBgImg(BufferedImage bgImg) {
		this.bgImg = bgImg;
	}
	/**
	 * @return the buttonList
	 */
	public ArrayList<Button> getButtonList() {
		return buttonList;
	}
	/**
	 * @param buttonList the buttonList to set
	 */
	public void setButtonList(ArrayList<Button> buttonList) {
		this.buttonList = buttonList;
	}
	/**
	 * @return the spriteList
	 */
	public ArrayList<Sprite> getSpriteList() {
		return spriteList;
	}
	/**
	 * @param spriteList the spriteList to set
	 */
	public void setSpriteList(ArrayList<Sprite> spriteList) {
		this.spriteList = spriteList;
	}

	/**
	 * @return the width
	 */
	public int getWidth() {
		return width;
	}

	/**
	 * @param width the width to set
	 */
	public void setWidth(int width) {
		this.width = width;
	}

	/**
	 * @return the height
	 */
	public int getHeight() {
		return height;
	}

	/**
	 * @param height the height to set
	 */
	public void setHeight(int height) {
		this.height = height;
	}
}
